package com.yunhe.estatemanage.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.estatemanage.domain.ProcessStatus;
import com.yunhe.estatemanage.domain.ReportRepair;
import com.yunhe.estatemanage.model.ReportRepairCondition;
import com.yunhe.estatemanage.model.ReportRepairHandle;
import com.yunhe.estatemanage.model.ReportRepairStatistics;
import com.yunhe.estatemanage.model.ReportRepairView;
import com.yunhe.estatemanage.service.ReportRepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ReportRepairController
 * @Description:
 * @author: GongDaCheng
 * @Date: 9:30 2021/9/3
 */
@Api(value = "ReportRepair", description = "报事报修", tags = {"Model_ReportRepair"})
@RestController
@RequestMapping(value = "/reportRepair")
//@RequestMapping(value = "/yh-estatemanage/reportRepair")
public class ReportRepairController {

    @Autowired
    private ReportRepairService reportRepairService;


    @ApiOperation("新增报事报修(小程序)")
    @PostMapping("/createReportRepair")
    public ResponseEntity<ResultObject> createReportRepair(@RequestBody ReportRepair reportRepair) throws ArgumentErrorException {
        return new ResponseEntity<>(new ResultObject<>(reportRepairService.create(reportRepair)), HttpStatus.OK);
    }

    @ApiOperation("根据id删除报事报修")
    @DeleteMapping("/deleteReportRepair")
    public ResponseEntity<ResultObject> deleteReportRepair(@RequestParam(value = "id",required = true) Long id){
        return new ResponseEntity<>(new ResultObject<>(reportRepairService.delete(id)), HttpStatus.OK);
    }

    @ApiOperation("更新报事报修")
    @PostMapping("/updateReportRepair")
    public ResponseEntity<ResultObject> updateReportRepair(@RequestBody ReportRepair reportRepair){
        return new ResponseEntity<>(new ResultObject<>(reportRepairService.update(reportRepair)), HttpStatus.OK);
    }

    @ApiOperation("根据id查询报事报修(web、App详细信息)")
    @GetMapping("/findReportRepairById")
    public ResponseEntity<ResultObject<ReportRepair>> findReportRepairById(@RequestParam(value = "id",required = true) Long id){
        return new ResponseEntity<>(new ResultObject(reportRepairService.findById(id)),HttpStatus.OK);
    }

    @ApiOperation("根据条件查询报事报修(web)")
    @PostMapping("/findReportRepairByConditionWeb")
    public ResponseEntity<ResultObject<List<ReportRepair>>> findReportRepairByConditionWeb(@RequestBody ReportRepairCondition reportRepairCondition){
        PageResult reportRepairByConditionWeb = reportRepairService.findReportRepairByConditionWeb(reportRepairCondition);
        return new ResponseEntity<>(new ResultObject(reportRepairByConditionWeb),HttpStatus.OK);
    }

    @ApiOperation("根据条件查询报事报修(小程序、App)")
    @PostMapping("/findReportRepairByConditionMob")
    public ResponseEntity<ResultObject<List<ReportRepair>>> findReportRepairByConditionMob(@RequestBody ReportRepairCondition reportRepairCondition) throws ArgumentErrorException {
        List<ReportRepair> reportRepairByConditionMob = reportRepairService.findReportRepairByConditionMob(reportRepairCondition);
        return new ResponseEntity<>(new ResultObject(reportRepairByConditionMob),HttpStatus.OK);
    }

    @ApiOperation("根据条件查询报事报修分页(小程序、App)")
    @PostMapping("/findReportRepairByConditionMobPaging")
    public ResponseEntity<ResultObject<List<ReportRepair>>> findReportRepairByConditionMobPaging(@RequestBody ReportRepairCondition reportRepairCondition) throws ArgumentErrorException {
        PageResult reportRepairByConditionMob = reportRepairService.findReportRepairByConditionMobPaging(reportRepairCondition);
        return new ResponseEntity<>(new ResultObject(reportRepairByConditionMob),HttpStatus.OK);
    }

    @ApiOperation("报事报修统计分析(web)")
    @PostMapping("/reportRepairStatistics")
    public ResponseEntity<ResultObject<ReportRepairStatistics>> reportRepairStatistics(@RequestBody List<Long> stationIds){
        return new ResponseEntity<>(new ResultObject(reportRepairService.reportRepairStatistics(stationIds)),HttpStatus.OK);
    }

    @ApiOperation("分派事项(web)")
    @PutMapping("/distributeReportRepair")
    public ResponseEntity<ResultObject> distributeReportRepair(@RequestParam(value = "id", required = true) Long id,
                                                               @RequestParam(value = "distributeOrgId", required = true) Long distributeOrgId,
                                                               @RequestParam(value = "distributePersonId", required = false) Long distributePersonId) throws ArgumentErrorException {
        reportRepairService.distributeReportRepair(id, distributeOrgId, distributePersonId);
        return new ResponseEntity<>(new ResultObject("success"), HttpStatus.OK);
    }

    @ApiOperation("催办事项(web)")
    @PutMapping("/urgeReportRepair")
    public ResponseEntity<ResultObject> urgeReportRepair(@RequestParam(value = "id", required = true) Long id){
        reportRepairService.urgeReportRepair(id);
        return new ResponseEntity<>(new ResultObject("success"), HttpStatus.OK);
    }

    @ApiOperation("报事报修查看(web)")
    @GetMapping("/viewReportRepair")
    public ResponseEntity<ResultObject<ReportRepairView>> viewReportRepair(@RequestParam(value = "id", required = true) Long id){
        ReportRepairView reportRepairView = new ReportRepairView();
        ReportRepair reportRepair = reportRepairService.viewReportRepair(id);
        List<ProcessStatus> processList = reportRepairService.getProcessList(id);
        reportRepairView.setReportRepair(reportRepair);
        reportRepairView.setReportRepairProcess(processList);
        return new ResponseEntity<>(new ResultObject(reportRepairView),HttpStatus.OK);
    }

    @ApiOperation("测试切面")
    @GetMapping("/test")
    public ResponseEntity<ResultObject<ReportRepairView>> test(@RequestParam(value = "id", required = true) Long id){
        ReportRepairView reportRepairView = reportRepairService.test(id);
        return new ResponseEntity<>(new ResultObject(reportRepairView),HttpStatus.OK);
    }

    /**
     * targetStatus传值
     * 确认更新：2 (Processing 处理中)
     * 已完成：3 (Completed 已完成)
     * 中止：5 (Suspension 中止)
     * 退回：1 (To_Be_Distributed 待派发)
     * */
    @ApiOperation("处理报事报修事项(App)")
    @PutMapping("/handleReportRepair")
    public ResponseEntity<ResultObject> handleReportRepair(@RequestBody ReportRepairHandle reportRepairHandle) throws ArgumentErrorException {
        reportRepairService.handleReportRepair(reportRepairHandle);
        return new ResponseEntity<>(new ResultObject("success"), HttpStatus.OK);
    }

    @ApiOperation("查看报事报修反馈历史信息(App)")
    @GetMapping("/getFeedbackHistory")
    public ResponseEntity<ResultObject<List<ProcessStatus>>> getFeedbackHistory(@RequestParam(value = "id", required = true) Long id){
        return new ResponseEntity<>(new ResultObject(reportRepairService.getFeedbackHistory(id)),HttpStatus.OK);
    }

    @ApiOperation("查看节点历史信息(小程序)")
    @GetMapping("/getNodeHistory")
    public ResponseEntity<ResultObject<List<ProcessStatus>>> getNodeHistory(@RequestParam(value = "id", required = true) Long id){
        return new ResponseEntity<>(new ResultObject(reportRepairService.getNodeHistory(id)),HttpStatus.OK);
    }

    @ApiOperation("撤销报事报修事项(小程序)")
    @PutMapping("/revokeReportRepair")
    public ResponseEntity<ResultObject> revokeReportRepair(@RequestParam(value = "id", required = true) Long id) throws ArgumentErrorException {
        reportRepairService.revokeReportRepair(id);
        return new ResponseEntity<>(new ResultObject("success"), HttpStatus.OK);
    }
}
